package com.cty;

import com.cty.Repository.UserRepository;
import com.cty.pojo.User;
import com.cty.pojo.UserDTO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import java.util.Arrays;
import java.util.List;

@SpringBootTest(classes = Main.class)
public class testQuery {


    @Autowired
    private UserRepository userRepository;
    @Test
    public void test01() {
        User user = userRepository.selectById4(1L);
        System.out.println(user);
    }
    @Test
    public void test02() {
        List<User> users = userRepository.selectAll();
        users.forEach(System.out::println);
    }

    @Test
    public void test03() {
        userRepository.insert("王五","123456");
    }

    @Test
    public void test04() {
        userRepository.update("赵六","123456",22L);
    }
    @Test
    public void test05() {
        userRepository.delById(22L);
    }

    @Test
    //排序
    public void test06() {
        List<User> users = userRepository.selectAllOrder("id");
        users.forEach(System.out::println);
    }
    @Test
    //排序
    public void test07() {
        List<User> users = userRepository.selectAllOrder2(Sort.by(Sort.Order.desc("id")));
        users.forEach(System.out::println);
    }
    @Test
    //排序
    public void test08() {
        List<Object[]> objects = userRepository.selectIdAndUserNameLength(Sort.by(Sort.Order.desc("id")));
        for (Object[] object : objects) {
            System.out.println(Arrays.toString(object));
        }
    }
    @Test
    //分页
    public void test09() {
        Page<User> userPage = userRepository.selectAllPage(PageRequest.of(1, 10));
        // 每页的大小
        System.out.println("userPage.getSize() = " + userPage.getSize());
        // 页码
        System.out.println("userPage.getNumber() = " + userPage.getNumber());
        //数据的总条数
        System.out.println("userPage.getTotalPages() = " + userPage.getTotalPages());
        List<User> content = userPage.getContent();
        content.forEach(System.out::println);
    }


    //DTO查询
    @Test
    public void test10() {
        List<UserDTO> userDTOS= userRepository.selectUserNameLenAndId();
        for (UserDTO userDTO : userDTOS) {
            // 没有toString
            System.out.println(userDTO.getUserNameLen());
            System.out.println(userDTO.getId());
        }
    }
    @Test
    public void test11() {
        User user = userRepository.selectIf(null, "123456789");
        System.out.println("user = " + user);
    }
    @Test
    public void test12() {
        User user = userRepository.selectIf2(null, "123456789");
        System.out.println("user = " + user);
    }



}
